using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using System.Data;

using PlugNT.Safe;
using PlugNT.Database;
using PlugNT.Database.Common.Simple;
using PlugNT.Cms.Custom;
using PlugNT.Cms.BLL;
using PlugNT.App.Shop.Model;

namespace PlugNT.App.Shop.DAL
{
    public class Cart 
    {
        internal static string currTableName = WebConfig.TablePrefix + "s_cart";



        public bool Add(CartItemInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into " + currTableName + " (");
            strSql.Append("sys_model_id,user_id,goods_id,goods_attr_idlist,goods_attr_json,[quantity],[price],price_market,[money],");
            strSql.Append("money_attr,[state],create_time)");
            strSql.Append(" values (");
            strSql.Append(" " + model.sys_model_id.ToString() + ",");
            strSql.Append(" " + model.user_id.ToString() + ",");
            strSql.Append(" " + model.goods_id.ToString() + ",");
            strSql.Append("'" + StringHelper.SqlFilter(model.goods_attr_idlist) + "',");
            strSql.Append("'" + StringHelper.SqlFilter(model.goods_attr_json) + "',");
            strSql.Append(" " + model.quantity.ToString() + ",");
            strSql.Append(" " + model.price.ToString() + ",");
            strSql.Append(" " + model.price_market.ToString() + ",");
            strSql.Append(" " + model.money.ToString() + ",");
            strSql.Append(" " + model.money_attr.ToString() + ",");
            strSql.Append(" " + model.state.ToString() + ",");

            strSql.Append("'" + model.create_time.ToString() + "'");
            strSql.Append(")");

            return DbHelper.ExecuteSql(strSql.ToString()) > 0;
        }



        public bool Update(CartItemInfo model)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("update " + currTableName + " set ");
            strSql.Append("user_id=" + model.user_id.ToString() + ",");
            strSql.Append("goods_attr_idlist='" + PlugNT.Safe.StringHelper.SqlFilter(model.goods_attr_idlist) + "',");
            strSql.Append("goods_attr_json='" + PlugNT.Safe.StringHelper.SqlFilter(model.goods_attr_json) + "',");
            strSql.Append("[quantity]=" + model.quantity.ToString() + ",");
            strSql.Append("[money]=" + model.money.ToString());
            strSql.Append(" where id=" + model.id.ToString());

            return DbHelper.ExecuteSql(strSql.ToString()) > 0;
        }

        public bool Delete(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [" + currTableName + "] ");
            strSql.Append(" where id=" + id.ToString());

            return DbHelper.ExecuteSql(strSql.ToString()) > 0;
        }


        public static bool DeleteByUserId(int userId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [" + currTableName + "] ");
            strSql.Append(" where user_id=" + userId.ToString());

            return DbHelper.ExecuteSql(strSql.ToString()) > 0;
        }


        public static bool DeleteByDays(int days)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [" + currTableName + "] where ");
            strSql.Append(WebConfig.IsAccess ? " datediff('d',create_time,Now())=" : " datediff(day,create_time,getdate())=");
            strSql.Append(days.ToString());

            return DbHelper.ExecuteSql(strSql.ToString()) > 0;
        }



        private static IDataReader GetReader(int topCount, string colNames, string andWhere)
        {
            string sql = "select " + (topCount == -1 ? "" : "top " + topCount) + " " + colNames + " from " + currTableName + " where 1=1 " + andWhere;
            return DbHelper.ExecuteReader(sql);
        }



        public static DataTable GetList(int sysModelId, string andWhere)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select a.*,b.images,b.title,b.ext_goods_integral from ");
            strSql.Append(currTableName + " as a left join " + Article.GetTableName(sysModelId) + " as b ");
            strSql.Append("on a.goods_id=b.id ");
            strSql.Append(" where a.sys_model_id=" + sysModelId + andWhere.Replace(" and "," and a."));

            return DbHelper.GetDataTable(strSql.ToString());
        }


        public DataTable GetList(int sysModelId, int topCount, int currPageCount, string andWhere, LeftJoinInfo joininfo)
        {

            string sql = Sql.GetRePagingString(topCount, currPageCount, currTableName, "id", "*", " and sys_model_id=" + sysModelId + andWhere, joininfo);
            return DbHelper.GetDataTable(sql);
        }

        public int GetCount(int sysModelId, string andWhere)
        {
            string sql = "select count(1) from [" + currTableName + "] where sys_model_id=" + sysModelId + andWhere;
            object obj = DbHelper.GetSingle(sql);
            return obj == null ? 0 : int.Parse(obj.ToString());
        }

        public int GetTotal(int sysModelId, string colName, string andWhere)
        {
            string sql = "select sum(" + colName + ") from [" + currTableName + "] where sys_model_id=" + sysModelId + andWhere;
            object obj = DbHelper.GetSingle(sql);
            return obj == null ? 0 : int.Parse(obj.ToString());
        }


        private CartItemInfo GetCartItemInfo(IDataReader dr)
        {
            CartItemInfo model = null;
            if (dr != null)
            {
                if (dr.Read())
                {
                    model = new CartItemInfo();
                    model.id = int.Parse(dr["id"].ToString());
                    model.sys_model_id = int.Parse(dr["sys_model_id"].ToString());
                    model.user_id = int.Parse(dr["user_id"].ToString());
                    model.goods_id = int.Parse(dr["goods_id"].ToString());
                    model.goods_attr_idlist = dr["goods_attr_idlist"].ToString();
                    model.goods_attr_json = dr["goods_attr_json"].ToString();
                    model.quantity = int.Parse(dr["quantity"].ToString());
                    model.price = double.Parse(dr["price"].ToString());
                    model.price_market = double.Parse(dr["price_market"].ToString());
                    model.money = double.Parse(dr["money"].ToString());
                    model.money_attr = double.Parse(dr["money_attr"].ToString());
                    model.state = int.Parse(dr["state"].ToString());
                    model.create_time = DateTime.Parse(dr["create_time"].ToString());
                }
                dr.Close();
            }
            return model;
        }
        public CartItemInfo GetCartItemInfo(int id)
        {
            IDataReader dr = GetReader(1, "*", " and id=" + id.ToString());
            return GetCartItemInfo(dr);
        }

        public CartItemInfo GetCartItemInfo(int sysModelId, int userId, int goodsId)
        {
            IDataReader dr = GetReader(1, "*", " and sys_model_id=" + sysModelId + " and user_id=" + userId + " and goods_id=" + goodsId);
            return GetCartItemInfo(dr);
        }


    }
}

